package com.test.factory;

import com.test.definition.BeanDefinition;
import com.test.exception.BeansException;
import com.test.registry.BeanDefinitionRegistry;
import com.test.scope.Scope;

import java.util.HashMap;
import java.util.Map;

/**
 * 默认的可列出Bean的工厂类，继承了AbstractAutowireCapableBeanFactory以支持自动装配
 */
//TODO
public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory implements BeanDefinitionRegistry, ConfigurableListableBeanFactory{

    // 存储Bean名称和其定义的映射关系
    private final Map<String, BeanDefinition> beanDefinitionMap = new HashMap<>();

    /**
     * 注册Bean定义到工厂中
     *
     * @param beanName Bean的名称
     * @param beanDefinition Bean的定义
     */
    public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) {
        beanDefinitionMap.put(beanName, beanDefinition);
    }

    /**
     * 获取指定名称的Bean定义
     *
     * @param beanName Bean的名称
     * @return Bean的定义
     * @throws IllegalArgumentException 如果找不到对应的Bean定义
     */
    public BeanDefinition getBeanDefinition(String beanName) {
        BeanDefinition beanDefinition = beanDefinitionMap.get(beanName);
        if (beanDefinition == null) {
            throw new IllegalArgumentException("找不到名为 '" + beanName + "' 的BeanDefinition");
        }
        return beanDefinition;
    }

    @Override
    public void preInstantiateSingletons() throws BeansException {

    }

    /**
     * 检查是否包含指定名称的Bean定义
     *
     * @param beanName Bean的名称
     * @return 如果包含则返回true，否则返回false
     */
    public boolean containsBeanDefinition(String beanName) {
        return beanDefinitionMap.containsKey(beanName);
    }

    @Override
    public <T> Map<String, T> getBeansOfType(Class<T> type) throws BeansException {
        return Map.of();
    }

    /**
     * 获取所有Bean定义的名称数组
     *
     * @return 所有Bean定义的名称数组
     */
    public String[] getBeanDefinitionNames() {
        return beanDefinitionMap.keySet().toArray(new String[0]);
    }

    /**
     * 获取指定类型的所有Bean名称数组
     *
     * @param type Bean的类型
     * @return 指定类型的所有Bean名称数组
     */
    public String[] getBeanNamesForType(Class<?> type) {
        return beanDefinitionMap.entrySet().stream()
                .filter(entry -> type.isAssignableFrom(entry.getValue().getBeanClass()))
                .map(Map.Entry::getKey)
                .toArray(String[]::new);
    }

    @Override
    public <T> T getBean(String name, Object... args) throws BeansException {
        return (T) getBean(name);
    }

    @Override
    public Object createBean(Class<?> beanClass) throws BeansException {
        return null;
    }

    @Override
    public void autowireBean(Object existingBean, String beanName) throws BeansException {

    }

    @Override
    public Object appBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName) throws BeansException {
        return null;
    }

    @Override
    public Object appBeanPostProcessorsAfterInitialization(Object existingBean, String beanName) throws BeansException {
        return null;
    }

    @Override
    public BeanFactory getParentBeanFactory() {
        return null;
    }

    @Override
    public boolean containsLocalBean(String name) {
        return false;
    }

    @Override
    public void registerScope(String scopeName, Scope scope) {

    }

    @Override
    public Class<?> getType(String name) throws BeansException {
        return null;
    }

    @Override
    public void setParentBeanFactory(ConfigurableBeanFactory parentBeanFactory) {

    }
}
